﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace TfsWrapper.OperationModes
{
    internal abstract class OperationMode
    {
        private readonly string key;
        public static readonly OperationMode Client = new ClientOperationMode();
        public static readonly OperationMode Server = new ServerOperationMode();
        public static readonly OperationMode Standalone = new StandaloneOperationMode();

        protected OperationMode(string key)
        {
            this.key = key;
        }

        private static IEnumerable<OperationMode> All
        {
            get
            {
                yield return Client;
                yield return Server;
                yield return Standalone;
            }
        }

        public static OperationMode FromKey(string key)
        {
            try
            {
                return All.SingleOrDefault(mode => mode.key.Equals(key, StringComparison.OrdinalIgnoreCase));
            }
            catch (InvalidOperationException)
            {
                throw new NotSupportedException(string.Format("Operation mode {0} is not recognized", key));
            }
        }

        public override string ToString()
        {
            return string.Format("OperationMode: {0}", key);
        }

        public abstract void Execute(IEnumerable<string> args);
    }
}